home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4253 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Command line arg and segmentation fault
  5. Date: Fri, 2 Feb 1996 23:04:55 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9602022204.AA19286@dxmint.cern.ch>
  8. References: <311273B8.25BF@soleil.acomp.usf.edu>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. Chris Frenck <cfrenck@soleil.acomp.usf.edu> writes:
  14.  
  15. >I am a beginning C student and have run into a bit of a problem. When compiling on GCC 
  16. >(required) I get a segmentation fault. If I compile with MSVC++ at home it runs fine. 
  17.  
  18. Whoever required GCC did you a big favour.  Your program is broken, but
  19. your Microsoft platform at home couldn't even let you know this.
  20.  
  21. >Could someone please tell me what the error is?
  22.  
  23. It's a typical newbie error, mentioned in this newsgroup's FAQ file.
  24.  
  25. >void reverse(int count,char *argv[])
  26. >{   
  27. >    /* Variable Declarations */
  28. >    char *argument;
  29. >    int tempctr;
  30. >    char *revphrase; 
  31. >    
  32. >    /* Function Prototype */
  33. >    char palin(char [], char []);    
  34. >    
  35. >      for (tempctr=count; count > 1; count--)
  36. >          { argument= argv[count];
  37. >               palin(argument,revphrase); 
  38.                                ^^^^^^^^^
  39. Huh???  You're passing to palin() an UNINITIALIZED pointer!!!
  40.  
  41. >              printf("%s ", revphrase);
  42. >            tempctr++;
  43. >          } 
  44. >          
  45. >    return;
  46. >     
  47. >}
  48. >
  49. >char palin(char *argument, char *revphrase)
  50. >{
  51. >   /* Variable declarations */
  52. >    int ctr1=0;
  53. >    int ctr2=0;    
  54. >                                /*SEGMENTATION FAULT IN FOR LOOP    */
  55. >                /* I also get a seg. fault if just  */
  56. >                /* the program name is entered and  */
  57. >                /* nothing else                */
  58. >
  59. >    for (ctr1 = strlen (argument)-1; ctr1 >=0; ctr1--) /*reverses*/
  60. >        { revphrase[ctr1]= argument[ctr2];
  61.                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  62. Here's the place where the segmentation fault occurs.  The program tries
  63. to store a character at a random memory address (revphrase has never been
  64. initialized to point to a memory area belonging to your program) and this
  65. is causing the segmentation fault.
  66.  
  67. Dan
  68. -- 
  69. Dan Pop
  70. CERN, CN Division
  71. Email: danpop@mail.cern.ch 
  72. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  73.